import sqlite3
import pokeparser

class table():
	def __init__(self):
		self.parser = pokeparser.pokeparser()
		self.conn = sqlite3.connect('pokedex.db')
		self.curs = self.conn.cursor()
	
	def createdex(self):
		self.curs.execute('''
		CREATE TABLE dex (
			id TEXT PRIMARY KEY,
			cn TEXT,
			jp TEXT,
			en TEXT,
			type1 TEXT,
			type2 TEXT,
			ab1 TEXT,
			ab2 TEXT,
			ab3 TEXT,
			high FLOAT,
			weigh FLOAT,
			gender FLOAT,
			hp INTEGER,
			at INTEGER,
			df INTEGER,
			sa INTEGER,
			sd INTEGER,
			sp INTEGER
		)
		''')
		self.conn.commit()
	def insertdex(self):
		query = 'INSERT INTO dex VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)'
		for line in open('text/dex.txt',encoding='utf-8'):
			line = self.transdex(self.parser.convert(line))
			try:
				self.curs.execute(query, line)
			except:
				pass
		for line in open('text/form.txt',encoding='utf-8'):
			line = self.transdex(self.parser.convert(line))
			try:
				self.curs.execute(query, line)
			except:
				pass
		self.conn.commit()
	def transdex(self, fields):
		try:
			type1 = type2 = ''
			index = fields[4].find('/')
			if index == -1:
				type1 = fields[4]
			else:
				types =  fields[4].split('/')
				type1 = types[0]
				type2 = types[1]
		except:
			type1 = type2 = ''
		try:
			ability1 = ability2 = hiddenability = ''
			index = fields[5].find('/')
			if index == -1:
				ability1 = ability2 = hiddenability = fields[5]
			else:
				abilitys = fields[5].split('/')
				ability1 = abilitys[0]
				ability2 = abilitys[1]
				if ability2 == '':
					ability2 = ability1
				try:
					hiddenability = abilitys[2]
				except:
					hiddenability = ''
		except:
			ability1 = ability2 = hiddenability = ''
		try:
			high = float(fields[6].strip('m'))
			weigh = float(fields[7].strip('kg'))
		except:
			high = weigh = -1
		try:
			gender = 0
			if fields[8] == '-1':
				gender = -1
			else:
				gender = float(fields[8].strip('%')) / 100
		except:
			gender = -100
		try:
			species = fields[9].split('/')
			hp = int(species[0])
			at = int(species[1])
			df = int(species[2])
			sa = int(species[3])
			sd = int(species[4])
			sp = int(species[5])
		except:
			hp = at = df = sa = sd = sp = 0
		return [fields[0],fields[1],fields[2],fields[3],type1,type2,ability1,ability2,hiddenability,high,weigh,gender,hp,at,df,sa,sd,sp]

	def createmove(self):
		self.curs.execute('''
		CREATE TABLE move (
			id TEXT PRIMARY KEY,
			cn TEXT,
			jp TEXT,
			en TEXT,
			type TEXT,
			class TEXT,
			power TEXT,
			acc TEXT,
			pp TEXT,
			info TEXT,
			range TEXT,
			effect TEXT
		)
		''')
		self.conn.commit()
	def insertmove(self):
		query = 'INSERT INTO move VALUES (?,?,?,?,?,?,?,?,?,?,?,?)'
		for line in open('text/move.txt',encoding='utf-8'):
			line = self.transmove(self.parser.convert(line))
			try:
				self.curs.execute(query, line)
			except:
				pass
		self.conn.commit()
	def transmove(self, fields):
		mid = fields[0]
		if mid != '—':
			mid = fields[0]
		else:
			mid = fields[1]
		return [mid,fields[1],fields[2],fields[3],fields[4],fields[5],fields[6],fields[7],fields[8],fields[9],fields[10],fields[11]]

	def createlearn(self):
		self.curs.execute('''
		CREATE TABLE learn (
			pid TEXT PRIMARY KEY,
			moves TEXT
		)
		''')
		self.conn.commit()
	def insertlearn(self):
		query = 'INSERT INTO learn VALUES (?,?)'
		for line in open('text/listlearn.txt',encoding='utf-8'):
			line = self.parser.convert(line)
			try:
				self.curs.execute(query, line)
			except:
				pass
		self.conn.commit()

	def createitem(self):
		self.curs.execute('''
		CREATE TABLE item (
			cn TEXT PRIMARY KEY,
			jp TEXT,
			en TEXT,
			des TEXT,
			bag TEXT,
			type TEXT,
			info TEXT,
			fling TEXT,
			effect TEXT
		)
		''')
		self.conn.commit()
	def insertitem(self):
		query = 'INSERT INTO item VALUES (?,?,?,?,?,?,?,?,?)'
		for line in open('text/listitem.txt',encoding='utf-8'):
			line = self.parser.convert(line)
			try:
				self.curs.execute(query, line)
			except:
				pass
		self.conn.commit()

	def createability(self):
		self.curs.execute('''
		CREATE TABLE ability (
			id INTEGER PRIMARY KEY,
			cn TEXT,
			jp TEXT,
			en TEXT,
			des TEXT,
			num INTEGER,
			hide INTEGER,
			info TEXT,
			effect TEXT
		)
		''')
		self.conn.commit()
	def insertability(self):
		query = 'INSERT INTO ability VALUES (?,?,?,?,?,?,?,?,?)'
		for line in open('text/ability.txt',encoding='utf-8'):
			line = self.parser.convert(line)
			try:
				self.curs.execute(query, line)
			except:
				pass
		self.conn.commit()

def main():
	table1 = table()
	table1.createdex()
	table1.insertdex()
	table1.createmove()
	table1.insertmove()
	table1.createlearn()
	table1.insertlearn()
	table1.createitem()
	table1.insertitem()
	table1.createability()
	table1.insertability()
	table1.conn.close()

if __name__ == "__main__":
	main()